<?php

/**
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     @license http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

$currentDir = dirname(__FILE__);
require_once($currentDir . "/sensapilityservice.php");
require_once($currentDir . "/utilities.php");

/**
 * @author api.jmatt@gmail.com
 * @version 0.1
 */
class SensapilityChannel extends SensapilityService{

  /**
   * @param soapclientnusoap $soapClient the NuSOAP object that communicates 
   * with the service
   * @param array $namespaces associative array that matches namespace labels
   * to namespace URIs
   * @param array $defaultNamespaceLabel the namespace to be used with most
   * tags
   * @return none
   */
  function SensapilityChannel($soapClient, 
                              $namespaces, 
                              $defaultNamespaceLabel) {
    parent::SensapilityService($soapClient,
                               $namespaces,
                               $defaultNamespaceLabel);
  }

  /**
   * Creates a new URL or custom channel.
   * @param string $name For a custom channel, the name of that channel, for
   * a URL channel, the URL that corresponds to that channel
   * @param string $status Either 'Active' or 'Inactive'
   * @param string $serviceId The syndication service id that this channel will
   * be assigned to
   * @param string $urlOrCustom What type of channel this is, either 'Url' or
   * 'Custom'
   * @return Channel_Data A Channel_Data object with the channel id set
   */
  function createChannel($name, $status, $serviceId, $urlOrCustom) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':id' => -1,
                            $ns . ':name' => $name,
                            $ns . ':type' => $urlOrCustom,
                            $ns . ':status' => $status,
                            $ns . ':synServiceId' => $serviceId);
    $callParameters = array($ns . ':channel' => 
                              array("value" => $callParameters));

    return parent::doSoapCall('createChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Create multiple channels in one request
   * @param array $channelArray An associative array formatted like a 
   * Channel_Data object
   * @return array An array of Channel_Data objects 
   */
  function createChannels($channelArray) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";

    $channels = array_keys($channelArray);
    foreach ( $channels as $thisChannel ) {
      $channelObj = $channelArray[$thisChannel];
      $singleChannel = array($ns . ':id' => -1,
                             $ns . ':name' => $channelObj['name'],
                             $ns . ':status' => $channelObj['status'],
                             $ns . ':synServiceId'=>$channelObj['synServiceId'],
                             $ns . ':type' => $channelObj['type']);
      $singleChannel = array($ns . ':channel' =>array("value"=>$singleChannel));
      $callParameters .= ArrayToXML($singleChannel);
    }
    
    return parent::doSoapCall('createChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * Removes the specified channel
   * @param long $killTarget The id of the channel to be removed.
   * @return none
   */ 
  function deleteChannel($killTarget) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $killTarget);

    return parent::doSoapCall('deleteChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Removes specified channels
   * @param array $killTargets an array of longs which are channel ids to 
   * remove
   * @return none
   */
  function deleteChannels($killTargets) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";
    $keys = array_keys($killTargets);
    foreach ( $keys as $key ) {
      $tempArray = array($ns . ':channelIds' => $killTargets[$key]);
      $callParameters .= ArrayToXML($tempArray);
    }

    return parent::doSoapCall('deleteChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * Retrieves information for specified channel
   * @param long $channelId The numeric identifier for this channel
   * @return Channel_Data a Channel_Data object that represents the requested 
   * channel
   **/
  function getChannel($channelId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $channelId);

    return parent::doSoapCall('getChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              1,
                              true);
  }

  /**
   * Retrieves information for specified channels
   * @param array $channelIds Array of numeric identifiers for channels
   * @return array Array of Channel_Data objects for the channels requested
   */
  function getChannels($channelIds) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";
    $keys = array_keys($channelIds);
    foreach ( $keys as $key ) {
      $tempArray = array($ns . ':channelIds' => $channelIds[$key]);
      $callParameters .= ArrayToXML($tempArray);
    }

    return parent::doSoapCall('getChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              true,
                              2,
                              true);
  }

  /**
   * Gets the service a given channel belongs to
   * @param long $channelId The numeric ID of the channel in question
   * @return SyndicationService_Data A SyndicationService_Data object for the
   * service that this channel belongs to.
   */
  function getSyndicationService($channelId) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':channelId' => $channelId);

    return parent::doSoapCall('getSyndicationService',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              true,
                              0,
                              true);
  }

  /**
   * Update attributes of a channel
   * @param long $channelId The channel to update
   * @param string $name The name of the channel, for Url channels, this is
   * the URL of the page corresponding to the channel
   * @param string $status 'Active' or 'Inactive'
   * @param string $synServiceId The service id for the account this channel
   * belongs to
   * @param string $type The type of channel this is, note this can never be 
   * updated
   * @return none
   */
  function updateChannel($channelId, $name, $status, $synServiceId, $type) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = array($ns . ':id' => $channelId,
                            $ns . ':name' => $name,
                            $ns . ':status' => $status,
                            $ns . ':synServiceId' => $synServiceId,
                            $ns . ':type' => $type);
    $callParameters = array('channel' => array('value' => $callParameters));

    return parent::doSoapCall('updateChannel',
                              ArrayToXml($callParameters),
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }

  /**
   * The same as update channel, but updates multiple channels in a single call
   * @param array $channelArray Array of Channel_Data objects with the
   * update information
   * @return none
   */
  function updateChannels($channelArray) {
    $ns = $this->defaultNamespaceLabel;
    $returnValue = null;
    $callParameters = "";

    $channels = array_keys($channelArray);
    foreach ( $channels as $thisChannel ) {
      $channelObj = $channelArray[$thisChannel];
      $singleChannel = array($ns . ':id' => $channelObj['id'],
                             $ns . ':name' => $channelObj['name'],
                             $ns . ':status' => $channelObj['status'],
                             $ns . ':synServiceId'=>$channelObj['synServiceId'],
                             $ns . ':type' => $channelObj['type']);
      $singleChannel = array($ns .':channels' =>
                                 array("value"=>$singleChannel) );
      $callParameters .= ArrayToXML($singleChannel);
    }

    return parent::doSoapCall('updateChannels',
                              $callParameters,
                              $this->__soapClient->namespaces[$ns],
                              false,
                              0,
                              true);
  }
}
?>
